package com.js.service.bot.core;

import com.js.mapper.bot.StorageMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class StorageService {

    @Resource
    private StorageMapper storageMapper;

    @Cacheable(value = "day", key = "#qq+#key")
    public String getStorageValue(Long qq, String key){
        return storageMapper.getStorageValue(qq, key);
    }

    @CacheEvict(value = "day", key = "#qq+#key")
    @Transactional(rollbackFor = Exception.class)
    public boolean addStorage(Long qq, String key, String value){
        try {
            deleteStorage(qq, key);
            storageMapper.addStorage(qq, key, value);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @CacheEvict(value = "day", key = "#qq+#key")
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteStorage(Long qq, String key){
        try {
            storageMapper.deleteStorage(qq, key);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
